package com.xbn.xbnmall.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.xbn.common.to.mq.StockDetailTo;
import com.xbn.common.to.mq.StockLockTo;
import com.xbn.common.utils.R;
import com.xbn.xbnmall.ware.dao.WareSkuDao;
import com.xbn.xbnmall.ware.entity.WareOrderTaskDetailEntity;
import com.xbn.xbnmall.ware.entity.WareOrderTaskEntity;
import com.xbn.xbnmall.ware.feign.OrderFeignService;
import com.xbn.xbnmall.ware.service.WareOrderTaskDetailService;
import com.xbn.xbnmall.ware.service.WareOrderTaskService;
import com.xbn.xbnmall.ware.service.WareSkuService;
import com.xbn.xbnmall.ware.vo.OrderVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author XBN
 * @date 2020/8/25 10:27
 * @email 78742541@qq.com
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
    @Autowired
    WareSkuService wareSkuService;
    @Autowired
    private WareSkuDao wareSkuDao;
    @Autowired
    OrderFeignService orderFeignService;
    @Autowired
    WareOrderTaskDetailService wareOrderTaskDetailService;
    @Autowired
    WareOrderTaskService wareOrderTaskService;
    /*
     *消息队列释放库存 ，订单超时等原因自动解锁
     * */
    @RabbitHandler
    public  void handleStockLockedRelease(StockLockTo to, Message message, Channel channel) throws IOException {
   try {
       wareSkuService.unlockStock(to);
       channel.basicAck(message.getMessageProperties().getDeliveryTag(),
               false);
   }catch (Exception e)
   {
       channel.basicReject(message.getMessageProperties().getDeliveryTag(),
               true);
   }





    }


}
